<script setup>
import { reactive, ref, onMounted } from 'vue'
import { useWardrobesStore } from '../stores/wardrobes'
import { ElMessage } from 'element-plus'
import { Tools } from '@element-plus/icons-vue'

const store = useWardrobesStore()
const submitting = ref(false)
const isDesktop = ref(false)

const form = reactive({
  id: '',
  order_time: '',
  customer_name: '',
  customer_phone: '',
  address: '',
  has_elevator: false,
  install_time: '',
  notes: '',
})

async function submit() {
  if (!form.id || !form.order_time || !form.customer_name || !form.customer_phone || !form.address || !form.install_time) {
    ElMessage.error('请填写必填项')
    return
  }
  
  submitting.value = true
  try {
    await store.upsertInstallation(Number(form.id), {
      order_time: form.order_time,
      customer_name: form.customer_name,
      customer_phone: form.customer_phone,
      address: form.address,
      has_elevator: form.has_elevator,
      install_time: form.install_time,
      notes: form.notes,
    })
    
    ElMessage.success('安装信息提交成功！')
    Object.assign(form, {
      id: '', order_time: '', customer_name: '', customer_phone: '',
      address: '', has_elevator: false, install_time: '', notes: ''
    })
  } catch (error) {
    ElMessage.error('提交失败：' + error.message)
  } finally {
    submitting.value = false
  }
}

// 检测设备类型
function detectDevice() {
  const userAgent = navigator.userAgent.toLowerCase()
  const isMobile = /mobile|android|iphone|ipad|phone/i.test(userAgent)
  const isTablet = /tablet|ipad/i.test(userAgent)
  const screenWidth = window.innerWidth
  
  isDesktop.value = !isMobile && !isTablet && screenWidth > 1024
}

onMounted(() => {
  detectDevice()
  window.addEventListener('resize', detectDevice)
})
</script>

<template>
  <div class="installation-container" :class="{ 'desktop': isDesktop }">
    <div class="page-header">
      <h1>🔧 待安装 - 订单信息</h1>
      <p>记录客户订单和安装相关信息</p>
    </div>

    <el-card class="form-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <span class="card-title">安装信息录入</span>
          <el-tag type="primary" size="small">待安装状态</el-tag>
        </div>
      </template>

      <el-form :model="form" label-width="140px" class="installation-form">
        <el-form-item label="衣柜ID" required>
          <el-input 
            v-model="form.id" 
            placeholder="请输入衣柜ID"
            class="form-input"
          />
        </el-form-item>

        <el-divider content-position="left">
          <span class="divider-title">📅 时间信息</span>
        </el-divider>

        <div class="form-grid">
          <el-form-item label="下单时间" required>
            <el-date-picker 
              v-model="form.order_time" 
              type="datetime" 
              placeholder="选择下单时间"
              format="YYYY-MM-DD HH:mm"
              value-format="YYYY-MM-DD HH:mm:ss"
              class="form-input"
            />
          </el-form-item>

          <el-form-item label="安装时间" required>
            <el-date-picker 
              v-model="form.install_time" 
              type="datetime" 
              placeholder="选择安装时间"
              format="YYYY-MM-DD HH:mm"
              value-format="YYYY-MM-DD HH:mm:ss"
              class="form-input"
            />
          </el-form-item>
        </div>

        <el-divider content-position="left">
          <span class="divider-title">👤 客户信息</span>
        </el-divider>

        <div class="form-grid">
          <el-form-item label="客户姓名" required>
            <el-input 
              v-model="form.customer_name" 
              placeholder="请输入客户姓名"
              class="form-input"
            />
          </el-form-item>

          <el-form-item label="联系电话" required>
            <el-input 
              v-model="form.customer_phone" 
              placeholder="请输入联系电话"
              class="form-input"
            />
          </el-form-item>
        </div>

        <el-divider content-position="left">
          <span class="divider-title">📍 安装地址</span>
        </el-divider>

        <el-form-item label="安装地址" required>
          <el-input 
            v-model="form.address" 
            placeholder="请输入详细的安装地址"
            type="textarea"
            :rows="3"
            class="form-input"
          />
        </el-form-item>

        <el-divider content-position="left">
          <span class="divider-title">⚙️ 安装条件</span>
        </el-divider>

        <div class="form-grid">
          <el-form-item label="是否有电梯">
            <el-switch 
              v-model="form.has_elevator" 
              active-text="有电梯"
              inactive-text="无电梯"
              class="elevator-switch"
            />
          </el-form-item>

          <el-form-item label="备注要求">
            <el-input 
              v-model="form.notes" 
              placeholder="请输入特殊安装要求或备注"
              type="textarea"
              :rows="3"
              class="form-input"
            />
          </el-form-item>
        </div>

        <el-form-item class="submit-section">
          <el-button 
            type="primary" 
            size="large" 
            @click="submit"
            :loading="submitting"
            class="submit-btn"
          >
            <el-icon><Tools /></el-icon>
            提交安装信息
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="info-tips">
      <el-alert
        title="提示信息"
        type="info"
        :closable="false"
        show-icon
      >
        <template #default>
          <ul>
            <li>提交后衣柜状态将变更为"待安装"</li>
            <li>请确保客户联系信息准确无误</li>
            <li>安装地址请填写详细的门牌号</li>
            <li>电梯信息将影响安装费用计算</li>
          </ul>
        </template>
      </el-alert>
    </div>
  </div>
</template>

<style scoped>
.installation-container {
  max-width: 1000px;
  margin: 0 auto;
}

.installation-container.desktop {
  max-width: none;
  margin: 0;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
  color: white;
}

.page-header h1 {
  margin: 0 0 10px 0;
  font-size: 2.5rem;
  font-weight: 700;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.page-header p {
  margin: 0;
  font-size: 1.1rem;
  opacity: 0.9;
}

.form-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #303133;
}

.installation-form {
  padding: 20px 0;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin: 20px 0;
}

.form-input {
  width: 100%;
}

.elevator-switch {
  margin-left: 10px;
}

.divider-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: #606266;
}

.submit-section {
  text-align: center;
  margin-top: 40px;
}

.submit-btn {
  padding: 15px 40px;
  font-size: 1.1rem;
  border-radius: 25px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.info-tips {
  margin-top: 20px;
}

.info-tips ul {
  margin: 10px 0 0 0;
  padding-left: 20px;
}

.info-tips li {
  margin-bottom: 5px;
  color: #606266;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header h1 {
    font-size: 2rem;
  }
  
  .form-grid {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .submit-btn {
    width: 100%;
    padding: 15px 20px;
  }
}

@media (max-width: 480px) {
  .page-header h1 {
    font-size: 1.8rem;
  }
  
  .page-header p {
    font-size: 1rem;
  }
  
  .form-card {
    margin: 0 10px 20px 10px;
  }
  
  .installation-form {
    padding: 15px 0;
  }
}
</style>


